package com.wanho.qz103.hrsaas.sys.dao.impl;

import com.wanho.qz103.hrsaas.commons.annontations.Repository;
import com.wanho.qz103.hrsaas.commons.dao.impl.BaseDAOImpl;
import com.wanho.qz103.hrsaas.commons.exception.DAOException;
import com.wanho.qz103.hrsaas.commons.exception.DataAccessException;
import com.wanho.qz103.hrsaas.commons.query.Page;
import com.wanho.qz103.hrsaas.sys.dao.RoleDAO;
import com.wanho.qz103.hrsaas.sys.pojos.Permission;
import com.wanho.qz103.hrsaas.sys.pojos.Role;

import java.util.List;

/**
 * dao 实现类： 提供CRUD的 SQL
 * 该类 交个工厂管理 ,并告诉 工厂该类为 数据层处理类
 */
@Repository
public class RoleDAOImpl extends BaseDAOImpl<Role> implements RoleDAO {
    @Override
    public boolean deleteRolePermissionRef(String roleId) throws DAOException {
        try {
            String sql="delete from sys_role_permission where roleId=?";
            Object[] paramAy = {roleId} ;
            return jdbcTemplate.update(sql,paramAy) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException("根据角色id删除关系表异常！！！") ;
        }
    }

    @Override
    public boolean insertBatchRolePermissionRef(Role role) throws DAOException {
        try {
            String sql="insert into sys_role_permission(roleId,permId) values (?,?)";
            //获得当前角色关联的权限对象
            List<Permission> permissionList = role.getPermissionList();
            Object[][] paramAy = new Object[permissionList.size()][2] ;
            //循环 赋值
            for (int i = 0; i < permissionList.size(); i++) {
                paramAy[i][0] = role.getId() ;
                paramAy[i][1] = permissionList.get(i).getId() ;
            }
            return jdbcTemplate.insertBatch(sql,paramAy) ;
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException("批量插入关系表异常！！！") ;
        }
    }

    @Override
    public List<Role> selectByUserId(String userId) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "\tid,\n" +
                    "\tNAME,\n" +
                    "\tregion \n" +
                    "FROM\n" +
                    "\tsys_role \n" +
                    "WHERE\n" +
                    "\tid IN ( SELECT roleId FROM sys_user_role WHERE userId = ? )" ;
            Object[] paramAy = {userId} ;
            return jdbcTemplate.queryList(sql,Role.class,paramAy);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException("根据用户id查询角色异常！") ;
        }
    }
    @Override
    protected String deleteSQL() {
        return "delete from  sys_role where id=#{id}";
    }

    @Override
    protected String insertSQL() {
        // #{?} 实体类中 pojos属性名  调用实例：getter方法的返回值
        return "insert into sys_role (id,name,region) values(#{id},#{name},#{region})";
    }

    @Override
    protected String updateSQL() {
        return "update sys_role set name=#{name},region=#{region} where id=#{id}";
    }

    @Override
    protected String selectByIdSQL() {
        return "SELECT id,name,region FROM sys_role where id=#{id}";
    }

    @Override
    protected String selectCountSQL() {
        return "SELECT count(*) FROM sys_role";
    }

    @Override
    protected String selectTopNSQL(Page page) {
        return "SELECT id,name,region FROM sys_role limit #{offset},#{size}" ;
    }

    @Override
    protected String selectAllSQL() {
        return "SELECT id,name,region FROM sys_role";
    }


}
